package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppFilterTypePopLayoutBinding;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.view.ui.adapter.transport.GoodsTypeAdapter3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/9/1
 */
public class FilterTypePopWindow extends BasePopWindow {

    private GoodsTypeAdapter3 goodsTypeAdapter;
    private GoodsInfoTypeEntity3 currentItem;
    private AppFilterTypePopLayoutBinding mBinding;
    private String title;

    public FilterTypePopWindow(Context context) {
        super(context);
    }

    public FilterTypePopWindow(Context context, String title) {
        super(context);
        this.title = title;
    }

    @Override
    protected int popLayout() {
        return R.layout.app_filter_type_pop_layout;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (AppFilterTypePopLayoutBinding) holder.getmBinding();

        goodsTypeAdapter = new GoodsTypeAdapter3(mContext, null);
        goodsTypeAdapter.setItemSelect(true);
        goodsTypeAdapter.setDefItem(0);
        mBinding.typeList.setLayoutManager(new GridLayoutManager(mContext, 4));
        mBinding.typeList.setAdapter(goodsTypeAdapter);

        goodsTypeAdapter.setOnItemClick(new AdapterOnItemClick<GoodsInfoTypeEntity3>() {
            @Override
            public void onItemClick(GoodsInfoTypeEntity3 provinceEntity, int position) {
                onResultClick.result(provinceEntity, position, null);
                currentItem = provinceEntity;
                dissMiss();
            }
        });
    }

    public void setData(List<GoodsInfoTypeEntity3> list, int position) {
        goodsTypeAdapter.setDatas(list);
        goodsTypeAdapter.setDefItem(position);

        if (!TextUtils.isEmpty(title) && mBinding != null) {
            mBinding.explainTv.setVisibility(View.VISIBLE);
            mBinding.explainTv.setText(title);
        }
    }

    public void setDefItem(int position) {
        goodsTypeAdapter.setDefItem(position);
    }

    public boolean isSetGoodstype() {
        return currentItem != null;
    }

    public String getGoodsTypeStr() {
        return currentItem.getName();
    }

    public void resetSelectItem() {
        goodsTypeAdapter.setDefItem(0);
        goodsTypeAdapter.resetCheckDatas();
    }

}
